//
//  CategoryDetailViewController.m
//  JZMCFM
//
//  Created by MJL on 15/10/31.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "CategoryDetailViewController.h"
#import "ClassifyHelper.h"
#import "CategoryModel.h"

#import "CategoryDetailCellModel.h"
#import "CategoryCellHeaderModel.h"
#import "CategoryDetailTableViewCell.h"

#import "CategoryPlayListViewController.h"


static NSString *cell_id = @"CategoryDetailTableViewCell";
@interface CategoryDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *CategoryDetailTableView;
@property(nonatomic,strong)NSMutableArray *cellArray;

@property(nonatomic,strong)UILabel * titleLabel;
@property(nonatomic,assign)NSInteger indexcount;

@property(nonatomic,strong)CategoryModel *cateModel;

@end

@implementation CategoryDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = NO;
    
    CategoryModel *cateModel = [ClassifyHelper sharedInstance].categoryArray[self.index];
    self.title = cateModel.title;
    self.cateModel = cateModel;
    NSLog(@"%@",cateModel.name);
    self.CategoryDetailTableView.dataSource = self;
    self.CategoryDetailTableView.delegate = self;
    
    //注册cell
    [self.CategoryDetailTableView registerNib:[UINib nibWithNibName:@"CategoryDetailTableViewCell" bundle:nil] forCellReuseIdentifier:cell_id];
    
    
    //头部解析
    [[ClassifyHelper sharedInstance]downLoadHeaderDataWithString:[NSString stringWithFormat:@"%@%ld%@",CategoryDetailUrl,cateModel.ID,CategoryDetailUrl2] withBlock:^{
        [self.CategoryDetailTableView reloadData];
    }];
    
    //请求数据
    [self downLoadData];
    //上拉刷新
    [self UpdateUp];
    //下拉刷新
    [self UpdateDown];
}

//请求数据
- (void)downLoadData
{
    NSURL *strurl = [NSURL URLWithString:[NSString stringWithFormat:@"%@category_name=%@&condition=hot&device=iPhone&page=%ld%@",PlayListUrl1,self.cateModel.name,self.indexcount,PlayListUrl2]];
    NSURLRequest *request = [NSURLRequest requestWithURL:strurl];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dict1 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *listAray = dict1[@"list"];
        for (NSDictionary *dict in listAray) {
            CategoryDetailCellModel *model = [[CategoryDetailCellModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.cellArray addObject:model];
        }
        [self.CategoryDetailTableView reloadData];
    }];
}


//上拉刷新
- (void)UpdateUp
{
    self.indexcount = 1;
    self.CategoryDetailTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.indexcount ++;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self downLoadData];
            [self.CategoryDetailTableView.footer endRefreshing];
        });
    }];
}



#pragma mark 下拉刷新
- (void)UpdateDown
{
    self.CategoryDetailTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self downLoadData];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.CategoryDetailTableView.header endRefreshing];
        });
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cellArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CategoryDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id forIndexPath:indexPath];
    CategoryDetailCellModel *model = self.cellArray[indexPath.row];
    [cell setCellDataWithModel:model];
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 110;
}



#pragma mark 自定义cell的头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        SDCycleScrollView *view = (SDCycleScrollView *) [[ClassifyHelper sharedInstance] LBImageView:[ClassifyHelper sharedInstance].categoryHeaderArray];
        self.titleLabel.text = nil;
        return view;
    }else{
        UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
        
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 200, 30)];
        [titleView addSubview:self.titleLabel];
        self.titleLabel.font = [UIFont systemFontOfSize:18.0];
        
        CategoryCellHeaderModel *model = [ClassifyHelper sharedInstance].categoryCellHeaderArray[section];
        self.titleLabel.text = model.title;
        return titleView;
    }
    
}





- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 180;
    }else{
        return 35;
    }
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CategoryPlayListViewController *playVC = [[CategoryPlayListViewController alloc]init];
    
    playVC.index = indexPath.row;
    playVC.cellDataArr = self.cellArray;
    [self.navigationController pushViewController:playVC animated:YES];
}


- (NSMutableArray *)cellArray
{
    if (!_cellArray) {
        _cellArray = [NSMutableArray array];
    }
    return _cellArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
